package com.symbio.ishenapi.doctor.controller;

import com.symbio.ishenapi.common.controller.BaseController;
import com.symbio.ishenapi.doctor.model.TmGroupInfo;
import io.jboot.web.controller.annotation.RequestMapping;
import vo.GroupMembersVO;

import java.util.List;
import java.util.Map;

/**
 * @Author: chen
 * @Description:科研组管理控制层
 * @Date: Created in 12:15 2018/11/1
 * @Modified By:
 */
@RequestMapping("/TmGroupInfo")
public class TmGroupInfoController extends BaseController{

    /**
     * 科研组列表分页查询
     */
    public void findList(){

        try {
            String departmentId = getPara("departmentId");
            Map map = TmGroupInfo.dao.findList(getQueryModel(),departmentId);
            renderPageJsonRet(map.get("count"),map.get("data"));
        }catch (Exception e){
            renderJsonError(40002,"科研组查询");
        }
    }

    /**
     * 科研组删除
     */
    public void delete(){

        int id = getParaToInt("id");
        int status = 0;

        try {
            //删除科研组信息
            status = TmGroupInfo.dao.deleteGroupInfo(id);
            //2018-12-06 分组信息和成员信息不在同一端，不做同时处理  ---chen
            //当科研组表数据删除成功时同时删除组成员
            /*if(status>0){
                TmGroupMembers.dao.deleteMembers(id);
            }*/
            renderJsonError(50001,"科研组删除");
        }catch (Exception e){
            renderJsonError(50002,"科研组删除");
        }

    }

    /**
     *添加分组同时添加成员
     */
    // TODO: 2018/11/28 成员表已迁移到后台，此接口暂时不可用
    public void saveGroupMembers(){
        GroupMembersVO groupMembersVO = (GroupMembersVO)getReqBodyObject(GroupMembersVO.class);
        try {
            int count = TmGroupInfo.dao.queryByName(groupMembersVO.getGroupName());
            if(count==0){
                //TmGroupInfo.dao.saveGroupMembers(groupMembersVO);
                renderJsonError(50001,"科研组添加");
            }else{
                renderJsonError(50002,"科研组已存在");
                return;
            }
        }catch (Exception e){
            renderJsonError(50002,"科研组添加");
        }
    }

    /**
     * 添加分组
     */
    public void saveGroup(){
        try {
            TmGroupInfo tmGroupInfo = (TmGroupInfo)getReqBodyObject(TmGroupInfo.class);
            TmGroupInfo.dao.saveGroup(tmGroupInfo);
            renderJsonError(50001,"分组添加");
        }catch (Exception e){
            renderJsonError(50002,"分组添加");
        }
    }

    /**
     * chen
     * 通过组ID获取名称
     * groupId 组id
     */
    public void getGroupNameById(){
        try {
            String groupId = getPara("groupId");
            String groupName = TmGroupInfo.dao.getGroupNameById(groupId);
            renderJsonRet(groupName);
        }catch (Exception e){
            renderJsonError(50002,"获取分组名称");
        }
    }

    /**
     * author:chen
     * 根据组id查询组详细信息
     * param:   id    组id
     * date:2018/11/20
     */
    public void findGroupById(){
        try {
            int id = getParaToInt("id");
            renderJsonRet(TmGroupInfo.dao.findGroupById(id));
        }catch (Exception e){
            renderJsonError(50002,"组详细查询失败");
        }
    }

    /** POST
     * @Author: chen
     * @Description:保存分组并返回分组id
     * @Date: 17:14 2018/11/28
     * @param: []
     * @return: void
     */
    public void saveGroupToId(){
        try {
            TmGroupInfo tmGroupInfo = (TmGroupInfo)getReqBodyObject(TmGroupInfo.class);
            TmGroupInfo.dao.saveGroup(tmGroupInfo);
            TmGroupInfo queryTmGroupInfo = TmGroupInfo.dao.queryTmGroupInfo(tmGroupInfo);
            renderJsonRet(queryTmGroupInfo);
        }catch (Exception e){
            renderJsonError(50002,"分组添加");
        }
    }

    /** POST
     * @Author: chen
     * @Description:分组信息修改
     * @Date: 11:10 2018/11/29
     * @param: []
     * @return: void
     */
    public void update(){
        try {
            TmGroupInfo tmGroupInfo = (TmGroupInfo)getReqBodyObject(TmGroupInfo.class);
            TmGroupInfo.dao.updateById(tmGroupInfo);
            renderJsonError(50001,"分组修改");

        }catch (Exception e){
            renderJsonError(50002,"分组修改");
        }
    }

    /** GET
     * @Author: chen
     * @Description:分组组长信息查询
     * @Date: 14:28 2018/11/29
     * @param: id   ---组id
     * @return: void  表单格式
     */
    public void findLeaderGroup(){
        try {
            int id = getParaToInt("id");
            Map<String,Object> map = TmGroupInfo.dao.findLeaderGroup(id);
            renderPageJsonRet(map.get("count"),map.get("data"));
        }catch (Exception e){
            renderJsonError(50002,"组长信息查询");
        }
    }

    /** GET
     * @Author: chen
     * @Description:判断此人是不是群组组长
     * @Date: 19:25 2018/12/3
     * @param: [
     *              groupId     ---群id
     *              doctorId    ---医生id
     *          ]
     * @return: true  是组长     false  不是组长
     */
    public void findIsLeader(){
        try {
            String groupId = getPara("groupId");
            String doctorId = getPara("doctorId");
            boolean isLeader = TmGroupInfo.dao.findIsLeader(groupId,doctorId);
            renderJsonRet(isLeader);
        }catch (Exception e){
            e.printStackTrace();
            renderJsonError(50002,"是否是组长查询");
        }
    }

    /** GET
     * @Author: chen
     * @Description:根据ids获取多个组信息
     * @Date: 12:23 2018/12/4
     * @param: [
     *              ids     ---组id集合 多个逗号分隔
     *              doctorId    ---登录医生id
     *          ]
     * @return: void
     */
    public void findGroupByIds(){
        try {
            String ids = getPara("ids");
            String doctorId = getPara("doctorId");
            List<TmGroupInfo> data = TmGroupInfo.dao.findGroupByIds(ids,doctorId);
            renderJsonRet(data);
        }catch (Exception e){
            e.printStackTrace();
            renderJsonError(50002,"获取群信息");
        }
    }
}
